package listings;

import classes.*;
import java.io.IOException;
import java.sql.SQLException;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 *
 * @author Alvaro
 */
public class listUsers extends BaseServlet {

    RequestDispatcher rd;

    /**
     * Processes requests for both HTTP
     * <code>GET</code> and
     * <code>POST</code> methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException, SQLException, ClassNotFoundException {

        HttpSession s = request.getSession();


        String redirect = "";

        redirect = request.getParameter("stats");

        


        //Parametros enviados en el formulario
        String busqueda = request.getParameter("viewOrder2");
        String by = request.getParameter("searchOrder2");


        AdminDB adb = new AdminDB();
        user u = new user();
        //Creacion de la lista donde se meten los usuarios que obtengamos en la busqueda
        List<user> users = null;


        
        if (redirect.equals("stats")) {

            //Listamos los customers
            //users = u.getAllUsuarios(adb.getConnection());
            users = u.searchUsers(adb.getConnection(), busqueda, "country");
            //Metemos en la sesion todos los pedidos obtenidos
            s.setAttribute("TopUsuarios", users);
            //Redirigimos
            response.sendRedirect("Admin/statistics.jsp");
        }
        
        else
        {
        //Metemos en la lista los resultados obtenidos
        users = u.searchUsers(adb.getConnection(), busqueda, by);

        System.out.println("Hemos llegado al server para listar los usuarios");
        //Metemos la lista en la sesion
        s.setAttribute("listaDeusuarios", users);
        //Redireccion
        response.sendRedirect("Admin/Admin.jsp");
        }


    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP
     * <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        try {
            try {
                processRequest(request, response);
            } catch (ClassNotFoundException ex) {
                Logger.getLogger(listUsers.class.getName()).log(Level.SEVERE, null, ex);
            }
        } catch (SQLException ex) {
            Logger.getLogger(listProducts.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    /**
     * Handles the HTTP
     * <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        try {
            try {
                processRequest(request, response);
            } catch (ClassNotFoundException ex) {
                Logger.getLogger(listUsers.class.getName()).log(Level.SEVERE, null, ex);
            }
        } catch (SQLException ex) {
            Logger.getLogger(listProducts.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
